
import QUANTAXIS as QA
# from QAStrategy import QAStrategyCTABase
from QAPUBSUB.consumer import subscriber_routing


class MLService(QAStrategyCTABase):

    def on_bar(self, bar):

        res = self.ml_factor()

        print(res.iloc[-1])

        if res.DIF[-1] > res.DEA[-1]:

            print('LONG')

            if self.positions.volume_long == 0:
                self.send_order('BUY', 'OPEN', price=bar['close'], volume=1)
            if self.positions.volume_short > 0:
                self.send_order('BUY', 'CLOSE', price=bar['close'], volume=1)

        else:
            print('SHORT')
            if self.positions.volume_short == 0:
                self.send_order('SELL', 'OPEN', price=bar['close'], volume=1)
            if self.positions.volume_long > 0:
                self.send_order('SELL', 'CLOSE', price=bar['close'], volume=1)

   

        return QA.QA_indicator_MACD(self.market_data)

    def risk_check(self):
        pass
        # pprint.pprint(self.qifiacc.message)


if __name__ == '__main__':
    ml_strategy = MLService(code='rbl8', frequence='1min',strategy_id='1dds1s2d-7902-4a85-adb2-fbac4bb977fe', start='2021-02-01', end='2021-02-02', model= 'rust')
    ml_strategy.run_backtest()
